package sad.controle;

import sad.util.Constantes;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.util.Vector;
import sad.dao.DAO;
import sad.dao.SubTopicoTableDAO;
import sad.dao.TopicoTableDAO;
import sad.modelo.Topico;
import sad.util.DAOEvent;
import sad.visao.CadSubTopicoDialog;
import sad.visao.DialogAbstrato;
import sad.modelo.SubTopico;

public class SubTopicoControle extends AbstractControl {

  private CadSubTopicoDialog v_s_Top;

  public SubTopicoControle(DialogAbstrato v) {
    this.v_s_Top = (CadSubTopicoDialog)v;
    addOuvinte();
    initComponents();
  }
  
// <editor-fold defaultstate="collapsed" desc="Responde a eventos com origem nas visões">
    @Override
    public void actionPerformed(ActionEvent e) {
        
        if(e.getActionCommand().equals(Constantes.SALVAR_SUBTOPICO)){
            createSubTopico();
        }else {
            if(e.getActionCommand().equals(Constantes.EXCLUIR)){
                removeSubTopico();
            }
            if(e.getActionCommand().equals(Constantes.UPDATE)){
                updateSubTopico();
            }
            SubTopicoTableDAO.getInstance().removeDAOOuvinte(this);
            v_s_Top.close();
        }
    }

    @Override
    public void windowClosing(WindowEvent e){
        switch(e.getID()){
           case WindowEvent.WINDOW_CLOSING:
                SubTopicoTableDAO.getInstance().removeDAOOuvinte(this);
                v_s_Top.close();
                break;
        }
    }// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Enchaminha eventos com origem no DAO">
    @Override
    public void daoChange(DAOEvent e) {
           v_s_Top.modelDAOChange(e);
    }// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Métodos privados + CRUD">
    private void addOuvinte() {
        v_s_Top.addActionListener(this);
        v_s_Top.addWindowOuvinte(this);
        SubTopicoTableDAO.getInstance().addDAOOuvinte(this);
    }

    private void initComponents() {
       Object[] listObjects = DAO.getListObjects(Topico.class);
       Vector<String> d = new Vector<String>();
       if (listObjects.length!=0){
           for (int i = 0; i < listObjects.length; i++) {
                Topico top = (Topico) listObjects[i];
                d.add(top.getNome()+"/"+top.getAbrev());
           }
           v_s_Top.reload(d);
       }
    }
    
    private void createSubTopico() {
            String nome  = v_s_Top.getNome();
            String abrev = v_s_Top.getAbreviatura();
            String t = v_s_Top.getTopico();
            
            if(verificarOK(nome, abrev, t, 0)) {
                Topico top = TopicoTableDAO.getTopicoSaved(t);
                SubTopicoTableDAO.getInstance().create(nome, abrev, top);
            }  
     }
    
    private void removeSubTopico() {
        SubTopicoTableDAO.getInstance().removeSubTopico();
    }

    private void updateSubTopico() {
            String nome  = v_s_Top.getNome();
            String abrev = v_s_Top.getAbreviatura();
            String t = v_s_Top.getTopico();

            if(verificarOK(nome, abrev, t, 1)){
                Topico top = TopicoTableDAO.getTopicoSaved(t);
                SubTopico s_top = SubTopicoTableDAO.getSubTopicoSaved(abrev);
                s_top.setNome(nome);
                s_top.setTopico(top);
                SubTopicoTableDAO.getInstance().update(s_top);
            }
    }

    private boolean verificarOK(String nome, String abrev, String t,  int tipo) {
            boolean ok = false;
            if (nome.equals("")){
                SubTopicoTableDAO.getInstance().erroField(nome, Constantes.NOME_SUBTOPICO);
            }else {
                if (abrev.equals("")){
                    SubTopicoTableDAO.getInstance().erroField(abrev, Constantes.ABREV_SUBTOPICO);
                }else{
                      if (t.equals("<Lista vazia>")){
                           SubTopicoTableDAO.getInstance().erroField(t, Constantes.SUBTOPICO);
                      }else{
                          if(tipo == 0){
                              Topico top = TopicoTableDAO.getTopicoSaved(t);
                              if(!SubTopicoTableDAO.getInstance().isExist(nome, abrev, top)) {
                                ok=true;
                            }
                          }else{
                           ok=true;   
                          }
                    }  
                }
            }
            return ok;
    }// </editor-fold>
}